<?php


class Loginvalidation
{
	/**
	 * @var $UserPassword, string
	 */
	private $postUsername;

	/**
	 * @var $postUserPassword, string
	 */
	private $loggedInSession;
	
	/**
	 * @var $postUserPassword, string
	 */
	private $postUserPassword;

	private static $cookieEndTimePath = "endtime.txt";

	private static $logedIn = "LoginValidation::logedIn";
	private static $lastEnteredUsername = "LoginValidation::lastEnteredUsername";
	private static $message = "LoginValidation::message";

	private static $ip = "LoginValidation::ip";
	private static $browser = "LoginValidation::browser";

	private static $browserValidation = "HTTP_USER_AGENT";
	private static $ipValidation = "REMOTE_ADDR";
	

	public function saveCookieEndTime($endtime)
	{
		file_put_contents(self::$cookieEndTimePath, $endtime);
	}

	public function getCookieEndTime()
	{
		return file_get_contents(self::$cookieEndTimePath);
	}

	/**
	 * @return boolean
	 */
    public function CheckLoginInfo($rememberMe) 
	{
		if(!empty($this->postUsername) && !empty($this->postUserPassword)&& $this->loggedInSession === false)
	    {
			$loggedIn = $this->LoginValidation();					  												
			
			if($loggedIn === false)
			{	
				$this->SetMessageSession('Fel användarnamn och/eller lösenord.');	
				$this->SetLastEnteredUsernameSession($this->postUsername);
				return false;
			}
			elseif($loggedIn === true)
			{	
				if($rememberMe) 
				   {
						$this->SetMessageSession('Du har loggat in, och vi kommer ihåg dig nästa gång.');
					}
					else 
					{   // lyckats logga in.
						$this->SetMessageSession('Du har loggat in.');	
					}
					$this->SetLoginSession(true);
					$this->SetLastEnteredUsernameSession($this->postUsername);
				return true;
			}
	    }
	}
	public function giveValues($username, $userPassword)
	{
		$this->postUsername = $username;
		$this->loggedInSession = $this->GetLoginSession();
		$this->postUserPassword = $userPassword;
		$this->SetMessageSession("");
	}

	

	public function weJustLoggedOut()
	{
		$this->SetLoginSession(false);
	}
	
	public function weJustLoggedIn($username)
	{
		$this->SetLastEnteredUsernameSession = $username;
		$this->SetLoginSession(true);
	}

	/**
	 * @param $username, string
	 * 
	 * @param $userPassword, string
	 * 
	 * @return boolean, true if correct loginInfo.
	 */
	public function LoginValidation()
	{	$username = $this->postUsername;
		$userPassword = $this->postUserPassword;

		if($username != "Admin" || $userPassword != "Password")
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	/**
	 * @param  $username, string
	 * 
	 * @param $userPassword, string
	 * 
	 * @return boolean, true if correct loginInfo.
	 */
	public function CookieLoginValidation($username, $userPassword)
	{
		if($username != "Admin" || $userPassword != "dc647eb65e6711e155375218212b3964")
		{
			return false;
		}
		else
		{
			return true;
		}
	}	


	/**
	 * @return boolean
	 */
	public function IsPasswordMissing()
	{
		// IF USERNAME BUT NO PASSWORD. 
		if(!empty($this->postUsername) && empty($this->postUserPassword))
		{
			$this->SetMessageSession("Lösenord saknas");
			$this->SetLastEnteredUsernameSession($this->postUsername);
			return true;
		}
			return false;
	}
	
	/**
	 * @return boolean
	 */
	public function IsUsernameMissing()
	{
		if(empty($this->postUsername))
		{
			$this->SetLastEnteredUsernameSession("");
			$this->SetMessageSession("Användarnamn saknas.");
			return true;
		}
		return false;
	}
	
	
	/**
	 * @return boolean
	 */
	public function CheckForSessionHijack()
	{
		if($this->GetUserAgentSession() != $_SERVER[self::$browserValidation] ||
    	   $this->GetIPSession()        != $_SERVER[self::$ipValidation])
		{
			$this->SetMessageSession("");
			$this->SetLoginSession(false);
			$this->SetLastEnteredUsernameSession(""); 
			return true;
		}
		return false;
	}


	public function DefaultIPandUserAgentValues()
	{
		if(isset($_SESSION[self::$browser]) == false)
		{
			  $_SESSION[self::$browser] = $_SERVER[self::$browserValidation];
		}
		 
		if(isset($_SESSION[self::$ip]) == false)
		{
			  $_SESSION[self::$ip] = $_SERVER[self::$ipValidation];
		}		
	}







	/**
	 * @Return string, ip adrs till användaren.
	 */
	public function GetIPSession()
	{
		 return $_SESSION[self::$ip];
		 
	}
	 
	 /**
	 * @Return string, vilken webbläsare.
	 */
	public function GetUserAgentSession()
	{
		
		 return $_SESSION[self::$browser];
		
	}
	
	/**
	 * @return bool
	 */
	public function GetLoginSession()
	{
		if(isset($_SESSION[self::$logedIn]))
		{
			return $_SESSION[self::$logedIn];
		}
		return FALSE;
	}
	
	/**
	 * @return string
	 */
	public function GetLastEnteredUsernameSession()
	{
		if(isset($_SESSION[self::$lastEnteredUsername]))
		{
			return htmlspecialchars($_SESSION[self::$lastEnteredUsername]);
		}
		else
		{
			return "";
		} 
		
	}
	
	/**
	 * @return string
	 */
	public function GetMessageSession()
	{
		if(isset($_SESSION[self::$message]))
		{
			return htmlspecialchars($_SESSION[self::$message]);
		}
		else
		{
			return "";
		} 
	}
	
	
	
	
	

	
	/**
	 * @param $loginBool, boolean,
	 */
	public function SetLoginSession($loginBool)
	{
		$_SESSION[self::$logedIn] = $loginBool;
	}
	
	/**
	 * @param  $lastEnteredUsername, string
	 */
	public function SetLastEnteredUsernameSession($lastEnteredUsername)
	{
		$_SESSION[self::$lastEnteredUsername] = $lastEnteredUsername;
	}
	
	/**
	 *@param  $message, string
	 */
	public function SetMessageSession($message)
	{
		$_SESSION[self::$message] = $message;
	}
	
}
